package com.huwei.boxuegu.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huwei.boxuegu.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class RegisterActivity extends AppCompatActivity {
    EditText mUsername,mPassword,mComfrimPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mUsername = (EditText) findViewById(R.id.usernameedt);
        mPassword = (EditText) findViewById(R.id.passwordedt);
        mComfrimPassword = (EditText) findViewById(R.id.password_comfim_edt);
    }

    public void back(View view){
        finish();
    }


    public void register(View view){
        //todo 点击的时候 两次密码的比较
//拿到两个输入框的内容

        if (TextUtils.isEmpty(mUsername.getText().toString())){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mPassword.getText().toString())){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (!mPassword.getText().toString().equals(mComfrimPassword.getText().toString())){
            Toast.makeText(this,"两次密码请输入一致",Toast.LENGTH_SHORT).show();
            return;
        }

        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Register");//请求的域名
        params.addQueryStringParameter("username",mUsername.getText().toString());
        params.addQueryStringParameter("password", mPassword.getText().toString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson  =new Gson();
                LoginModel loginModel = gson.fromJson(result,LoginModel.class);
                if (loginModel.code==200){//注册成功
                    Toast.makeText(RegisterActivity.this,"注册成功" ,Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(RegisterActivity.this,"注册失败" +loginModel.msg,Toast.LENGTH_SHORT).show();
                }
                Log.i("LoginActivity","onSuccess loginModel coce:" +loginModel.code);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("LoginActivity","onError result:" +ex.getMessage());
                Toast.makeText(RegisterActivity.this,"注册失败" ,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
